import json
from django.http import HttpResponse
from django.shortcuts import render
from service import dataService

state = {}


def hello(request):
    return render(request, "index.html")


def getData(request):
    datalist = dataService.getDataList()
    return HttpResponse(json.dumps(datalist))


def add(request):
    if not dataService.getDataList():
        state['code'] = dataService.spiderData()
    else:
        state['code'] = '数据已存在'
    return HttpResponse(json.dumps(state))


def delete(request):
    if dataService.getDataList():
        state['code'] = dataService.deleteData()
    else:
        state['code'] = '数据不存在'
    return HttpResponse(json.dumps(state))
